package edu.chl.tdaniel.ws2;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import edu.chl.tdaniel.ws2.core.User;
import edu.chl.tdaniel.ws2.db.Database;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Mikey
 */
@WebServlet(name = "LoginServlet", urlPatterns = {"/login", "/webshop/login"})
public class LoginServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
        String login = request.getParameter("login");
        String passwd = request.getParameter("passwd");
        String action = request.getParameter("action");
            
            
            if (action.equals("login")){
                
                User usr = Database.getUser(login, passwd); 
                
                if (usr != null){
                    request.getSession().setAttribute("user", usr);
                    request.setAttribute("msg", "Successful login!");
                    request.getRequestDispatcher("main.jspx").forward(request, response);
                } else {
                    request.setAttribute("msg", "Failed to login! Need to register?");
                    request.getRequestDispatcher("register.jspx").forward(request, response);
                }
            }
            
            else if (action.equals("register")) {
                
                if(Database.addUser(login, passwd)){
                    
                    request.setAttribute("msg", "New user registered: "+login);
                    request.getRequestDispatcher("main.jspx").forward(request, response);
             
                } else {
                    
                    request.setAttribute("msg", "The user "+login+" already exists!");
                    request.getRequestDispatcher("register.jspx").forward(request, response);
                }
                
            }
            
            else if (action.equals("logout")) {
                HttpSession s = request.getSession();
                if (s.getAttribute("user") != null) {
                    //s.removeAttribute("user");
                    s.invalidate();
                }
                response.sendRedirect("main.jspx");

            }
            
            
           /* boolean newUser = true;
            if (request.getParameter("action").equals("login")) newUser = false;
            
            HttpSession s = request.getSession();
            User usr = Database.getUser(login, passwd);
           
            
            // attempt to register as a user who's not in database
            if (newUser==true && usr == null) {
                Database.addUser(login, passwd);
                out.println("<html><body>Added user: "+login+"</body></html>");
            }
            // attempt to register as an existing user
            else if (newUser==true && usr != null) {
                response.sendRedirect("register.jspx");
            }
            // the user is logged in.
            else if (newUser==false && usr != null) {
                
                s.setAttribute("user", usr);
                out.println("<html><body>Session: "+s.getId()+","+s.getAttribute("user") +"</body></html>");
            }
            else
            {
                //the user is sent to registration page
                response.sendRedirect("register.jspx");*/
            }
                         

    

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
